﻿Imports VendasBLL
Imports VendasDAL
Public Class ClienteFRM
    Dim ObjBLL As New ClientesBLL
    Public Op As String
    Public codigo As Integer
#Region "Carrega Campos"
    Sub CarregaCampos(codigo As Integer)
        ObjBLL = New ClientesBLL(codigo)
        LBCodigo.Text = "Código: " & ObjBLL.Codigo
        LBTitulo.Text = ObjBLL.Nome
        CBTipo.SelectedValue = ObjBLL.tpe_codigo
        TXTComplemento.Text = ObjBLL.Complemento
        TXTNome.Text = ObjBLL.Nome
        MTBCep.Text = ObjBLL.Cep
        MTBCPF.Text = ObjBLL.cpf
        TXTRG.Text = ObjBLL.rg
        MTBCNPJ.Text = ObjBLL.cnpj
        TXTIe.Text = ObjBLL.inscricao
        TXTBairro.Text = ObjBLL.Bairro
        TXTCidade.Text = ObjBLL.Cidade
        TXTEndereco.Text = ObjBLL.Endereco
        TXTNum.Text = ObjBLL.Num
        CBUf.SelectedValue = ObjBLL.Uf
        MTBResidencial.Text = ObjBLL.telres
        MTBCelular.Text = ObjBLL.telcel
        MTBComercial.Text = ObjBLL.telcom
        MTBFax.Text = ObjBLL.telfax
        TXTObs.Text = ObjBLL.Obs
        TXTEmail.Text = ObjBLL.Email
        CBStatus.SelectedValue = ObjBLL.Status
        DTPNascimento.Text = ObjBLL.DtNascimento

    End Sub
#End Region

#Region "Limpar campos"
    Sub Limpa()
        CBStatus.SelectedValue = 1
        TXTNome.Text = ""
        MTBCPF.Text = ""
        TXTRG.Text = ""
        MTBCNPJ.Text = ""
        TXTIe.Text = ""
        TXTBairro.Text = ""
        TXTCidade.Text = ""
        TXTEndereco.Text = ""
        TXTNum.Text = ""
        CBUf.SelectedValue = ""
        MTBResidencial.Text = ""
        MTBCelular.Text = ""
        MTBComercial.Text = ""
        MTBFax.Text = ""
        TXTObs.Text = ""
        TXTEmail.Text = ""
        CBTipo.SelectedValue = 1
        MTBCep.Text = ""
        TXTComplemento.Text = ""
        DTPNascimento.Text = Date.Now
        Me.ObjBLL = Nothing
    End Sub
#End Region

#Region "Load"

    Private Sub ClienteFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        objconexao.carregaCombo(CBTipo, "Generico.Gen_TipoPessoa", "tpe_codigo", "tpe_descricao", "tpe_codigo < 3", "tpe_codigo")
        objconexao.carregaCombo(CBUf, "Generico.Gen_Estado", "est_uf", "est_descricao", "", "est_uf")
        Limpa()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LBTitulo.Text = "Cliente"
            LBCodigo.Text = 0

        Else
            LBCodigo.Visible = True
            CarregaCampos(codigo)
        End If
    End Sub
#End Region

#Region "Salvar/Alterar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click

        Try

            ObjBLL = New ClientesBLL(codigo, StrConv(Trim(TXTNome.Text), VbStrConv.ProperCase), StrConv(Trim(TXTEndereco.Text), VbStrConv.ProperCase), TXTNum.Text,
              StrConv(Trim(TXTBairro.Text), VbStrConv.ProperCase), StrConv(Trim(TXTCidade.Text), VbStrConv.ProperCase), CBUf.SelectedValue,
              StrConv(Trim(TXTComplemento.Text), VbStrConv.ProperCase), MTBCep.Text.Replace(" ", "").Replace("-", ""), Trim(TXTEmail.Text), Trim(TXTObs.Text),
              DTPNascimento.Text, CBStatus.SelectedValue, CBTipo.SelectedValue, Trim(MTBCPF.Text).Replace("-", "").Replace(" ", "").Replace(",", ""), Trim(TXTRG.Text), Trim(MTBCNPJ.Text).Replace("/", "").Replace("-", "").Replace(" ", "").Replace(",", ""), Trim(TXTIe.Text), Trim(MTBResidencial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
              Trim(MTBComercial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""), Trim(MTBCelular.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
             Trim(MTBFax.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""))

            If Op = "I" Then
                ObjBLL.InserirAlterar()
                MessageBox.Show("Cliente inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            Else
                ObjBLL.InserirAlterar()
                MessageBox.Show("Cliente alterado com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

    Private Sub CBTipo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBTipo.SelectedIndexChanged
        If CBTipo.SelectedIndex = 0 Then
            LBCnpj.Visible = False
            MTBCNPJ.Visible = False
            LBIe.Visible = False
            TXTIe.Visible = False
            LBCpf.Visible = True
            MTBCPF.Visible = True
            LBRG.Visible = True
            TXTRG.Visible = True
        ElseIf CBTipo.SelectedIndex = 1 Then
            LBCpf.Visible = False
            MTBCPF.Visible = False
            LBRG.Visible = False
            TXTRG.Visible = False
            LBCnpj.Visible = True
            MTBCNPJ.Visible = True
            LBIe.Visible = True
            TXTIe.Visible = True
        End If
        MTBCNPJ.Text = ""
        TXTIe.Text = ""
        TXTRG.Text = ""
        MTBCPF.Text = ""
    End Sub

#Region "Keypress de RG"
    Private Sub TXTRG_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTRG.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
#End Region

#Region "Keypress da inscrição estadual"
    Private Sub TXTIe_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTIe.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
#End Region

    Private Sub CBTipo_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CBTipo.KeyPress
        e.Handled = True
    End Sub

    Private Sub ClienteFRM_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
End Class